package com.lxb.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
import com.lxb.commonutils.vo.EduCourseVo;
import com.lxb.commonutils.vo.UcenterMemberVO;
import com.lxb.eduorder.client.EduClient;
import com.lxb.eduorder.client.UcenterClient;
import com.lxb.eduorder.entity.Order;
import com.lxb.eduorder.mapper.OrderMapper;
import com.lxb.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxb.eduorder.utils.OrderNoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单 服务实现类
 *
 * @author lixuebin
 * @since 2023-01-16
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 创建订单
     *
     * @param courseId 课程ID
     * @param id       会员ID
     * @return 返回订单id
     */
    @Override
    public String save(String courseId, String id) {

        //1.根据课程id远程调用service-edu模块，获取课程信息
        EduCourseVo courseInfoVO = eduClient.getCourseInfoVO(courseId);
        //2.根据用户id远程调用service-edu模块，获取用户信息
        UcenterMemberVO infoUc = ucenterClient.getInfoUc(id);

        //创建订单信息
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseInfoVO.getId());
        order.setCourseTitle(courseInfoVO.getTitle());
        order.setCourseCover(courseInfoVO.getCover());
        order.setTeacherName(courseInfoVO.getTeacherName());
        order.setMemberId(infoUc.getId());
        order.setNickname(infoUc.getNickname());
        order.setMobile(infoUc.getMobile());
        order.setEmail(infoUc.getEmail());
        order.setTotalFee(courseInfoVO.getPrice());
        order.setPayType(1);//支付类型（1：微信 2：支付宝）
        order.setStatus(0);//订单状态（0：未支付 1：已支付）


        //3.创建订单
        baseMapper.insert(order);
        //返回订单号
        String OrderId = order.getOrderNo();
        return OrderId;
    }
}
